package action;

import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import pojo.UserProfile;

import com.opensymphony.xwork2.ActionSupport;

import dao.UserDAO;

public class RegisterAction extends ActionSupport implements RequestAware, SessionAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3997101947172440485L;
	
	private static final Logger logger = Logger.getLogger(RegisterAction.class);
	
	@Autowired  
    private UserDAO UserDAO;   
	
	private Map<String, Object> request;
    private Map<String, Object> session;
    
    private UserProfile userProfile;
    private String rePassword;
	private String reEmail;

	public String execute()
	{
		if (userProfile == null)
			return ERROR;
		
		logger.warn("Test log for debug by Ngoc Son");
		
		if (userProfile.getFirstName().isEmpty() || userProfile.getLastName().isEmpty() ||
				userProfile.getEmail().isEmpty() || userProfile.getZipcode().isEmpty() ||
				userProfile.getPhoneNumber().isEmpty() || rePassword.isEmpty() || reEmail.isEmpty())
		{
			request.put("errorregister", "All fields must be are required !!!");
			return ERROR;
		}
		
		if (!userProfile.getPassword().equals(rePassword))
		{
			request.put("errorregister", "Password does not match RePassword !!!");
			return ERROR;
		}
		
		if (!userProfile.getEmail().equals(reEmail))
		{
			request.put("errorregister", "Email does not match ReEmail !!!");
			return ERROR;
		}
		
		if ( !UserDAO.addUserProfile(userProfile, true) )
		{
			request.put("errorregister", "Email is already exist !!!");
			return ERROR;
		}
		
		request.put("j_username", userProfile.getEmail());
		request.put("j_password", userProfile.getPassword());
		return SUCCESS;
	}
	
	public UserProfile getUserProfile() {
		return userProfile;
	}

	public void setUserProfile(UserProfile userProfile) {
		this.userProfile = userProfile;
	}

	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	public void setRequest(Map<String, Object> arg0) {
		this.request = arg0;
	}
	
	public String getRePassword() {
		return rePassword;
	}

	public void setRePassword(String rePassword) {
		this.rePassword = rePassword;
	}
	
	public String getReEmail() {
		return reEmail;
	}

	public void setReEmail(String reEmail) {
		this.reEmail = reEmail;
	}
}
